package com.scx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.scx.context.BaseContext;
import com.scx.dto.ShoppingCartDTO;
import com.scx.entity.Book;
import com.scx.entity.Setmeal;
import com.scx.entity.ShoppingCart;
import com.scx.mapper.BookMapper;
import com.scx.mapper.SetmealMapper;
import com.scx.mapper.ShoppingCartMapper;
import com.scx.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addshoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getUserId() != null,ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.eq(shoppingCart.getBookId() != null,ShoppingCart::getBookId, shoppingCart.getBookId());
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getBookGift() != null,ShoppingCart::getBookGift, shoppingCart.getBookGift());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(lqw);

        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //已存在商品,数量+1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart);
        }else{
            if(shoppingCart.getBookId()!=null){
                //是图书
                Book Book = bookMapper.selectById(shoppingCart.getBookId());
                shoppingCart.setName(Book.getName());
                shoppingCart.setAmount(Book.getPrice());
                shoppingCart.setImage(Book.getImage());

            }else{
                //是套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BaseContext.getCurrentId() != null,ShoppingCart::getUserId, BaseContext.getCurrentId());
        return shoppingCartMapper.selectList(lqw);
    }

    @Override
    public void clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BaseContext.getCurrentId() != null,ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartMapper.delete(lqw);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getUserId() != null,ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.eq(shoppingCart.getBookId() != null,ShoppingCart::getBookId, shoppingCart.getBookId());
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getBookGift() != null,ShoppingCart::getBookGift, shoppingCart.getBookGift());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(lqw);

        if(shoppingCartList != null && shoppingCartList.size() > 0) {
            shoppingCart = shoppingCartList.get(0);
            if (shoppingCart.getNumber() == 1){
                //只有一个，直接删除记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else{
                //有多个，数量-1
                shoppingCart.setNumber(shoppingCart.getNumber() -1 );
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
    }
}
